package com.dt.common.validation.parser;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSONObject;
import com.dt.common.validation.annotation.DateStr;
import com.dt.common.validation.definition.DateStrDefinition;
import com.dt.common.validation.exception.BuildingException;
import com.dt.common.validation.impl.DefaultValidationDefinition;
import com.dt.common.validation.itf.PropertyGetter;

public class DateStrDefinitionParser extends DefinitionParser {

	@Override
	public DefaultValidationDefinition parseFromAnnotation(Field field, PropertyGetter propertyGetter) {
		DateStr dateStr = field.getAnnotation(DateStr.class);
		if (dateStr != null) {
			DateStrDefinition definition = new DateStrDefinition();
			definition.setFormat(dateStr.format());
			definition.setRawMsg(dateStr.msg());

			return definition;
		}
		return null;
	}

	@Override
	public DefaultValidationDefinition parseFromJson(JSONObject metaValues) {
		String format = metaValues.getString("format");
		if (format == null || format.isEmpty()) {
			throw new BuildingException("DateStr format must defined.");
		}

		DateStrDefinition definition = new DateStrDefinition();
		definition.setFormat(format);
		definition.setRawMsg(this.getMsgDefinition(metaValues, true));

		return definition;
	}

}
